package com.idata.service.services.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import com.idata.model.response.DisputeWordResponse;
import com.idata.service.config.WordHotProperties;
import com.idata.service.services.DisputeHotWordService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DisputeHotWordServiceJiebaImpl implements DisputeHotWordService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DisputeHotWordServiceJiebaImpl.class);

    private JiebaSegmenter segmenter = new JiebaSegmenter();

    @Override
    public List<DisputeWordResponse> getHotWords(String text) {
        List<SegToken> process = segmenter.process(text, JiebaSegmenter.SegMode.INDEX);
        if (CollectionUtil.isEmpty(process)) {
            return new ArrayList<>();
        }
        Map<String, Long> cntByKey = process.stream().map(segToken -> segToken.word)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        List<DisputeWordResponse> result = cntByKey.entrySet().stream().
                map(entry -> new DisputeWordResponse(entry.getKey(), entry.getValue().intValue()))
                .collect(Collectors.toList());
        Collections.sort(result);
        return result;
    }
}
